home *** CD-ROM | disk | FTP | other *** search
- /* xllist - xlisp list builtin functions */
-
- #ifdef CI_86
- #include "a:stdio.h"
- #include "xlisp.h"
- #endif
-
- #ifdef AZTEC
- #include "a:stdio.h"
- #include "xlisp.h"
- #endif
-
- #ifdef unix
- #include <stdio.h>
- #include <xlisp.h>
- #endif
-
-
- /* external variables */
-
- extern struct node *xlstack;
-
-
- /* local variables */
-
- static struct node *t;
- static struct node *a_subr;
- static struct node *a_list;
- static struct node *a_sym;
- static struct node *a_int;
- static struct node *a_str;
- static struct node *a_obj;
- static struct node *a_fptr;
- static struct node *a_kmap;
-
-
- /**********************************
- * xlist - builtin function list *
- **********************************/
-
- static struct node *xlist(args)
- struct node *args;
- {
- struct node *oldstk,arg,list,val,*last,*lptr;
-
- oldstk = xlsave(&arg,&list,&val,NULL);
- arg.n_ptr = args;
-
- for (last = NULL; arg.n_ptr != NULL; last = lptr)
- {
- val.n_ptr = xlevarg(&arg.n_ptr);
- lptr = newnode(LIST);
- if (last == NULL)
- list.n_ptr = lptr;
- else
- last->n_listnext = lptr;
- lptr->n_listvalue = val.n_ptr;
- }
-
- xlstack = oldstk;
- return (list.n_ptr);
- }
-
-
- /*********************************
- * cond - builtin function cond *
- *********************************/
-
- static struct node *cond(args)
- struct node *args;
- {
- struct node *oldstk,arg,list,*val;
-
- oldstk = xlsave(&arg,&list,NULL);
- arg.n_ptr = args;
-
- val = NULL;
- while (arg.n_ptr != NULL)
- {
- list.n_ptr = xlmatch(LIST,&arg.n_ptr);
- if (xlevarg(&list.n_ptr) != NULL)
- {
- while (list.n_ptr != NULL)
- val = xlevarg(&list.n_ptr);
- break;
- }
- }
-
- xlstack = oldstk;
- return (val);
- }
-
-
- /****************************
- * atom - is this an atom? *
- ****************************/
-
- static struct node *atom(args)
- struct node *args;
- {
- struct node *arg;
-
- if ((arg = xlevarg(&args)) == NULL || arg->n_type != LIST)
- return (t);
- else
- return (NULL);
- }
-
-
- /*************************
- * null - is this null? *
- *************************/
-
- static struct node *null(args)
- struct node *args;
- {
- if (xlevarg(&args) == NULL)
- return (t);
- else
- return (NULL);
- }
-
-
- /**********************************
- * type - return type of a thing *
- **********************************/
-
- static struct node *type(args)
- struct node *args;
- {
- struct node *arg;
-
- if (!(arg = xlevarg(&args)))
- return (NULL);
-
- switch (arg->n_type)
- {
- case SUBR: return (a_subr);
-
- case LIST: return (a_list);
-
- case SYM: return (a_sym);
-
- case INT: return (a_int);
-
- case STR: return (a_str);
-
- case OBJ: return (a_obj);
-
- case FPTR: return (a_fptr);
-
- case KMAP: return (a_kmap);
-
- default: xlfail("Bad node.");
-
- }
- }
-
-
- /****************************
- * listp - is this a list? *
- ****************************/
-
- static struct node *listp(args)
- struct node *args;
- {
- if (xlistp(xlevarg(&args)))
- return (t);
- else
- return (NULL);
- }
-
-
- /*************************************
- * xlistp - internal listp function *
- *************************************/
-
- static int xlistp(arg)
- struct node *arg;
- {
- return (arg == NULL || arg->n_type == LIST);
- }
-
-
- /**************************
- * eq - are these equal? *
- **************************/
-
- static struct node *eq(args)
- struct node *args;
- {
- struct node *oldstk,arg,arg1,arg2,*val;
-
- oldstk = xlsave(&arg,&arg1,&arg2,NULL);
- arg.n_ptr = args;
-
- arg1.n_ptr = xlevarg(&arg.n_ptr);
- arg2.n_ptr = xlevarg(&arg.n_ptr);
- xllastarg(arg.n_ptr);
-
- if (xeq(arg1.n_ptr,arg2.n_ptr))
- val = t;
- else
- val = NULL;
-
- xlstack = oldstk;
- return (val);
- }
-
-
- /*******************************
- * xeq - internal eq function *
- *******************************/
-
- static int xeq(arg1,arg2)
- struct node *arg1,*arg2;
- {
- if (arg1 != NULL && arg1->n_type == INT &&
- arg2 != NULL && arg2->n_type == INT)
- return (arg1->n_int == arg2->n_int);
- else
- return (arg1 == arg2);
- }
-
-
- /*****************************
- * equal - are these equal? *
- *****************************/
-
- static struct node *equal(args)
- struct node *args;
- {
- struct node *oldstk,arg,arg1,arg2,*val;
-
- oldstk = xlsave(&arg,&arg1,&arg2,NULL);
- arg.n_ptr = args;
-
- arg1.n_ptr = xlevarg(&arg.n_ptr);
- arg2.n_ptr = xlevarg(&arg.n_ptr);
- xllastarg(arg.n_ptr);
-
- if (xequal(arg1.n_ptr,arg2.n_ptr))
- val = t;
- else
- val = NULL;
-
- xlstack = oldstk;
- return (val);
- }
-
-
- /*************************************
- * xequal - internal equal function *
- *************************************/
-
- static int xequal(arg1,arg2)
- struct node *arg1,*arg2;
- {
- if (xeq(arg1,arg2))
- return (TRUE);
- else
- if (xlistp(arg1) && xlistp(arg2))
- return (xequal(arg1->n_listvalue,arg2->n_listvalue) &&
- xequal(arg1->n_listnext, arg2->n_listnext));
- else
- return (FALSE);
- }
-
-
- /*************************************
- * head - return the head of a list *
- *************************************/
-
- static struct node *head(args)
- struct node *args;
- {
- struct node *list;
-
- if ((list = xlevmatch(LIST,&args)) == NULL)
- xlfail("null list");
-
- xllastarg(args);
-
- return (list->n_listvalue);
- }
-
-
- /*************************************
- * tail - return the tail of a list *
- *************************************/
-
- static struct node *tail(args)
- struct node *args;
- {
- struct node *list;
-
- if ((list = xlevmatch(LIST,&args)) == NULL)
- xlfail("null list");
-
- xllastarg(args);
-
- return (list->n_listnext);
- }
-
-
- /*******************************************
- * nth - return the nth element of a list *
- *******************************************/
-
- static struct node *nth(args)
- struct node *args;
- {
- struct node *oldstk,arg,list;
- int n;
-
- oldstk = xlsave(&arg,&list,NULL);
- arg.n_ptr = args;
-
- if ((n = xlevmatch(INT,&arg.n_ptr)->n_int) < 1)
- xlfail("invalid argument");
-
- if ((list.n_ptr = xlevmatch(LIST,&arg.n_ptr)) == NULL)
- xlfail("invalid argument");
-
- xllastarg(arg.n_ptr);
-
- for (; n > 1; n--)
- {
- list.n_ptr = list.n_ptr->n_listnext;
- if (list.n_ptr == NULL || list.n_ptr->n_type != LIST)
- xlfail("invalid argument");
- }
-
- xlstack = oldstk;
- return (list.n_ptr->n_listvalue);
- }
-
-
- /*****************************************
- * length - return the length of a list *
- *****************************************/
-
- static struct node *length(args)
- struct node *args;
- {
- struct node *oldstk,list,*val;
- int n;
-
- oldstk = xlsave(&list,NULL);
-
- list.n_ptr = xlevmatch(LIST,&args);
- xllastarg(args);
-
- for (n = 0; list.n_ptr != NULL; n++)
- list.n_ptr = list.n_ptr->n_listnext;
-
- xlstack = oldstk;
-
- val = newnode(INT);
- val->n_int = n;
- return (val);
- }
-
-
- /*************************************
- * append - builtin function append *
- *************************************/
-
- static struct node *append(args)
- struct node *args;
- {
- struct node *oldstk,arg,list,last,val,*lptr;
-
- oldstk = xlsave(&arg,&list,&last,&val,NULL);
- arg.n_ptr = args;
-
- while (arg.n_ptr != NULL)
- {
- list.n_ptr = xlevmatch(LIST,&arg.n_ptr);
- while (list.n_ptr != NULL && list.n_ptr->n_type == LIST)
- {
- lptr = newnode(LIST);
- if (last.n_ptr == NULL)
- val.n_ptr = lptr;
- else
- last.n_ptr->n_listnext = lptr;
- lptr->n_listvalue = list.n_ptr->n_listvalue;
- last.n_ptr = lptr;
- list.n_ptr = list.n_ptr->n_listnext;
- }
-
- if (list.n_ptr != NULL)
- xlfail("bad list");
- }
-
- xlstack = oldstk;
- return (val.n_ptr);
- }
-
-
- /***************************************
- * reverse - builtin function reverse *
- ***************************************/
-
- static struct node *reverse(args)
- struct node *args;
- {
- struct node *oldstk,list,val,*lptr;
-
- oldstk = xlsave(&list,&val,NULL);
-
- list.n_ptr = xlevmatch(LIST,&args);
- xllastarg(args);
-
- while (list.n_ptr != NULL && list.n_ptr->n_type == LIST)
- {
- lptr = newnode(LIST);
- lptr->n_listvalue = list.n_ptr->n_listvalue;
- lptr->n_listnext = val.n_ptr;
- val.n_ptr = lptr;
-
- list.n_ptr = list.n_ptr->n_listnext;
- }
-
- if (list.n_ptr != NULL)
- xlfail("bad list");
-
- xlstack = oldstk;
- return (val.n_ptr);
- }
-
-
- /*************************************
- * cons - construct a new list cell *
- *************************************/
-
- static struct node *cons(args)
- struct node *args;
- {
- struct node *oldstk,arg,arg1,arg2,*lptr;
-
- oldstk = xlsave(&arg,&arg1,&arg2,NULL);
- arg.n_ptr = args;
-
- arg1.n_ptr = xlevarg(&arg.n_ptr);
- arg2.n_ptr = xlevarg(&arg.n_ptr);
- xllastarg(arg.n_ptr);
-
- lptr = newnode(LIST);
- lptr->n_listvalue = arg1.n_ptr;
- lptr->n_listnext = arg2.n_ptr;
-
- xlstack = oldstk;
- return (lptr);
- }
-
-
- /************************************************
- * xllinit - xlisp list initialization routine *
- ************************************************/
-
- xllinit()
- {
- /* define some symbols */
- t = xlenter("t");
- a_subr = xlenter("SUBR");
- a_list = xlenter("LIST");
- a_sym = xlenter("SYM");
- a_int = xlenter("INT");
- a_str = xlenter("STR");
- a_obj = xlenter("OBJ");
- a_fptr = xlenter("FPTR");
- a_kmap = xlenter("KMAP");
-
- /* functions with reasonable names */
- xlsubr("head",head);
- xlsubr("tail",tail);
- xlsubr("nth",nth);
-
- /* real lisp functions */
- xlsubr("atom",atom);
- xlsubr("eq",eq);
- xlsubr("equal",equal);
- xlsubr("null",null);
- xlsubr("type",type);
- xlsubr("listp",listp);
- xlsubr("cond",cond);
- xlsubr("list",xlist);
- xlsubr("cons",cons);
- xlsubr("car",head);
- xlsubr("cdr",tail);
- xlsubr("append",append);
- xlsubr("reverse",reverse);
- xlsubr("length",length);
- }